﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;
using TrainingsApp.Logic;
using ZJFramework10.Extension;
using System.IO;
using TrainingsApp.Logic.Import;

namespace TrainingsApp.IPADApp.Master {
    public partial class TeachMaterialsImport : PageBase {


        public TS_TeachMaterials_Entity MaterialsEntity;


        public long MaterialsId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("mid");
            }
        }

        protected void Page_Load(object sender, EventArgs e) {



            cbxGrade.DataSource = TrainingsApp.Logic.TS_Grade_Logic.ListAll();
            cbxGrade.ValueField = "Idx";
            cbxGrade.ValueType = typeof(int);
            cbxGrade.TextField = "GradeName";
            cbxGrade.DataBind();
            if (MaterialsId > 0) {

                MaterialsEntity = TS_TeachMaterials_Logic.TS_TeachMaterials_View(MaterialsId);
                List<TS_TeachMaterials_Chapter_Entity> dataSource = TrainingsApp.Logic.TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_ListByMaterialsId(MaterialsId);
                this.aspTree.DataSource = dataSource;
                this.aspTree.DataBind();
            }
            else {
                aspTree.Visible = false;
            }
            this.cbxCourseInfo.DataSource = TrainingsApp.Logic.TS_CourseInfo_Logic.TS_CourseInfo_ListByPaging(100, 1).TS_CourseInfo_List;
            this.cbxCourseInfo.ValueField = "Idx";
            cbxCourseInfo.ValueType = typeof(int);
            this.cbxCourseInfo.TextField = "CourseName";
            this.cbxCourseInfo.DataBind();
            if (!IsPostBack) {
                if (MaterialsEntity != null) {
                    this.cbxCourseInfo.Value = MaterialsEntity.CourseId.Value;
                    this.cbxGrade.Value = MaterialsEntity.Grade;
                    this.txtName.Text = MaterialsEntity.Name;
                    this.txtCide.Text = MaterialsEntity.Code;

                }
            }


        }


        private void DataBinds() {

            List<TS_TeachMaterials_Chapter_Entity> dataSource = TrainingsApp.Logic.TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_ListByMaterialsId(1);
            this.aspTree.DataSource = dataSource;
            this.aspTree.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            if (MaterialsEntity != null) {
                string pyahisPath = ImportUploaderManager.Upload(fileIpload.PostedFile);
                if (!string.IsNullOrEmpty(pyahisPath)) {
                    ChapterTypeImport import = new ChapterTypeImport();
                    import.Import((int)MaterialsEntity.Idx.Value, pyahisPath, ImportType.Chapter);
                }
                else {
                    this.ltError.Text = "<span style='color: Red;'>请检查上传的知识点资料,大小不能超过4MB,支持格式为:xls,xlsx</span>";
                    return;
                }
                Response.Redirect("TeachMaterialsEdit.aspx" + "?mid=" + MaterialsEntity.Idx);
            }
            else {
                Response.Redirect("TeachMaterialsManager.aspx");
            }

        }

        protected void btnBack_Click(object sender, EventArgs e) {
            Response.Redirect("TeachMaterialsManager.aspx");
        }
    }


    public class ImportUploaderManager {
        const string ImageSavePath = "/upload/import/";
        public static string Upload(HttpPostedFile file) {
            if (file != null && IsInValidatorExtensions(file.FileName)
                && file.ContentLength <= 4 * 1024 * 1024) {

                string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                string tempFileName = string.Concat(HttpContext.Current.Server.MapPath(ImageSavePath), fileName);


                file.SaveAs(tempFileName);



                return tempFileName;
            }
            return string.Empty;
        }


        static readonly List<String> str = new List<string>() { 
        ".xls",
        ".xlsx"
        };
        public static bool IsInValidatorExtensions(string name) {
            string extension = Path.GetExtension(name);
            return !string.IsNullOrEmpty(str.FirstOrDefault(u => u.Equals(extension, StringComparison.CurrentCultureIgnoreCase)));
        }


    }
}